# -*- coding: utf-8 -*-
# coding=utf-8
from __future__ import print_function, absolute_import, unicode_literals
import numpy as np
import pandas as pd
from gm.api import *
import talib

'''
本策略首先计算了SHFE.rb1801过去300个1min收盘价的均值和标准差
并用均值加减2和3个标准差得到网格的区间分界线,分别配以0.3和0.5的仓位权重
然后根据价格所在的区间来配置仓位:
(n+k1*std,n+k2*std],(n+k2*std,n+k3*std],(n+k3*std,n+k4*std],(n+k4*std,n+k5*std],(n+k5*std,n+k6*std]
(n为收盘价的均值,std为收盘价的标准差,k1-k6分别为[-40, -3, -2, 2, 3, 40],其中-40和40为上下界,无实际意义)
[-0.5, -0.3, 0.0, 0.3, 0.5](资金比例,此处负号表示开空仓)
回测数据为:SHFE.rb1801的1min数据。
回测时间为:2017-07-01 08:00:00到2017-10-01 16:00:00
'''


def init(context):
    context.symbol = 'DCE.PP'
    # context.symbol = 'SHFE.ZN'  #
    # 订阅SHFE.rb1801, bar频率为1min
    subscribe(symbols=context.symbol, frequency='60s')
    # 获取过去300个价格数据
    timeseries = history_n(symbol=context.symbol, frequency='60s', count=300, fields='close', fill_missing='Last',
                           end_time='2017-07-01 08:00:00', df=True)['close'].values
    print('timeseries', timeseries)
    context.timeseries = timeseries
    # 获取网格区间分界线


def on_bar(context, bars):
    # bar = bars[0]

    ma_240 = talib.MA(context.timeseries, timeperiod=240)
    ma_62 = talib.MA(context.timeseries, timeperiod=62)
    ma_5 = talib.MA(context.timeseries, timeperiod=5)
    # print('ma_240', ma_240)
<<<<<<< HEAD

    # if ma_240[-1] < ma_62[-1] < ma_5[-1]:
    #     order_target_percent(symbol=context.symbol, percent=0.5, order_type=OrderType_Market,
    #                          position_side=PositionSide_Long)
    #     print('空', '差值：', ma_5[-1] - ma_240[-1])
    # elif ma_240[-1] > ma_62[-1] > ma_5[-1]:
    #     order_target_percent(symbol=context.symbol, percent=0.5, order_type=OrderType_Market,
    #                          position_side=PositionSide_Long)
    #     print('多', '差值：', ma_5[-1] ,ma_62[-1], ma_240[-1])
=======
    if ma_240[-1] < ma_62[-1] < ma_5[-1]:
        print('空', '差值：', end='')
        print(ma_240[-1])
        order_target_percent(symbol=context.symbol, percent=0.5, order_type=OrderType_Market,
                             position_side=PositionSide_Long)
        # print('空', '差值：',   end='')
        # print(ma_240[-1] )
    elif ma_240[-1] > ma_62[-1] > ma_5[-1]:

        order_target_percent(symbol=context.symbol, percent=0.5, order_type=OrderType_Market,
                             position_side=PositionSide_Long)
        # print('多', '差值：', ma_240)#ma_5[-1] ,ma_62[-1], ma_240[-1])

    print(ma_240[-1],ma_62[-1], ma_240[-1])
>>>>>>> 75014e03821968c4fd454c34aa10817da6b2b0e2


if __name__ == '__main__':
    run(strategy_id='e3485832-b668-11e9-8ef4-04d4c459e1d4',
        filename='均线策略.py',
        mode=MODE_BACKTEST,
        token='90be3f863b23ab3c1ef68d1f9b8dc06e4bebb30d',
        backtest_start_time='2017-07-01 08:00:00',
        backtest_end_time='2017-10-01 16:00:00',
        backtest_adjust=ADJUST_PREV,
        backtest_initial_cash=10000000,
        backtest_commission_ratio=0.0015,
        backtest_slippage_ratio=0.0001)
